﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itau.SC.EnterpriseLib.ExceptionHandling;
using System.ServiceModel;

namespace EnterpriseLibraryWrapper
{
    public class ExceptionHandlerWrapper : IExceptionHandlerWrapper
    {
        public bool HandleException(Exception ex, string policyName)
        {
            bool exceptionResult = ExceptionPolicy.HandleException(ex, policyName);

            if (ex.GetType() == Type.GetType("itau.im.dado.DadosException, Dados") && policyName.ToUpper().Equals("ERROSNEGOCIOS"))
            {
                throw new FaultException<WCFFaultContract>(new WCFFaultContract() { Message = ex.Message }, new FaultReason(ex.Message));
            }
            else if (ex.GetType() == Type.GetType("itau.im.negocio.NegociosException, Negocios") && policyName.ToUpper().Equals("ERROSNEGOCIOS"))
            {
                throw new FaultException<WCFFaultContract>(new WCFFaultContract() { Message = ex.Message }, new FaultReason(ex.Message));
            }
            else if (ex.GetType() == Type.GetType("System.Exception, mscorlib") && policyName.ToUpper().Equals("ERROSNEGOCIOS"))
            {
                throw new FaultException<WCFFaultContract>(new WCFFaultContract() { Message = "Erro genérico" }, new FaultReason("Erro genérico"));
            }

            return exceptionResult;
        }

        public bool HandleException(Exception ex, string policyName, out Exception exceptionToThrow)
        {
            return ExceptionPolicy.HandleException(ex, policyName, out exceptionToThrow);
        }
    }
}
